<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Services\Form\initFormData as InitFormData;
use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Industry extends Controller
{
    use \App\Traits\ApiTrait, \App\Traits\CacheTrait, \App\Traits\IndustryTrait;
    public $curl;
    public $request;
    public $formData;
    public $breadMenuList;
    private $apiCompanyListUri='ecology/chart/listCompany';
    private $apiCaseUri='projectcase/viewProjectCase';
    private $apiCompanyUri='ecology/chart/watchEnterprise';
    public $apiBaseUri='api.crm.tanwin.cn/';
    public function __construct(Request $request, InitFormData $formData)
    {
        $this->curl = new \Ixudra\Curl\CurlService();
        $this->request = $request;
        $this->formData = $formData;
        $this->breadMenuList = config('front.bread_menu');

    }

    public function index()
    {

        // if (env('CACHE_INDEX') && Cache::has('industry_index_view_data')) {
        //     $viewData = Cache::get('industry_index_view_data');
        //     return view('web.industry.index', $viewData);
        // }
        // $solutionData = $this->cacheSolutionData();
        // $greenResultData = $this->cacheGreenResultData();
        // $chainData = $this->cacheChainData();
        if(isset($_REQUEST['companyName']))
        $companyName=$_REQUEST['companyName'];
        else $companyName='NFZJ';
        if(isset(config('front.company')[$companyName]['title']) && config('front.company')[$companyName]['title']!='')

        $viewData = [
            'seo' => $this->returnSeoInfo('industry_index'),
            'selectedMenu' => parent::MENU_INDUSTRY_COO,
            'contentImg' => config('front.company')[$companyName]['img'],
            'contentTitle'=>config('front.company')[$companyName]['title'],
            'contentContent'=>config('front.company')[$companyName]['content'],
        ];
        else {
          $companyName='NFZJ';
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'contentImg' => config('front.company')[$companyName]['img'],
              'contentTitle'=>config('front.company')[$companyName]['title'],
              'contentContent'=>config('front.company')[$companyName]['content'],
          ];
        }
        Cache::forever('industry_index_view_data', $viewData);
        return view('web.industry.index', $viewData);
    }

    public function index124(Request $request){
      $viewData = [
          'seo' => $this->returnSeoInfo('industry_index'),
          'selectedMenu' => parent::MENU_INDUSTRY_COO,
      ];
      return view('web.industry.p_first', $viewData);
    }

    public function ajaxCompanyList(Request $request){
      $source=$request->source;
      $type=$request->type;
      if($source=='' || $source==null)$source='design_consult';
      if($type=='' || $type==null)$type='30';
      $ability=[
        'design_consult'=>'has_design_ability',
        'invest'=>'has_invest_ability',
        'device'=>'has_device_supply_ability',
        'construct'=>'has_construct_ability',
        'operation'=>'has_operation_ability'
      ];
      $abilityName=[
        'design_consult'=>'设计咨询',
        'invest'=>'金融投资',
        'device'=>'设备供应',
        'construct'=>'项目承建',
        'operation'=>'后续运维',
      ];
      $abilityStar=[
        'design_consult'=>'designconsult_star',
        'invest'=>'invest_star',
        'device'=>'device_star',
        'construct'=>'construct_star',
        'operation'=>'operation_star',
      ];
      $rqUrl=$this->apiBaseUri.$this->apiCompanyListUri;
      $rqData=['personal_mobile'=>'18058715658','fromRequest'=>'tanwin','pageNumber'=>1,'pageSize'=>10,'source'=>$source,$ability["$source"]=>'1','type'=>$type];
      $result=$this->curl->to($rqUrl)
      ->withOption('TIMEOUT', 10)
      ->withData($rqData)
      ->post();
      // print_r($result);
      // die;
      try {
        $result=json_decode($result,true);
        if($result['code']=='100'){
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'abilityName'=>$abilityName["$source"],
              'data'=>$result['data']['data'],
              'code'=>'100',
              'abilityStar'=>$abilityStar["$source"],
          ];
          return view('web.industry.list.ajaxList', $viewData);
        }
        else{
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'abilityName'=>$abilityName["$source"],
              'data'=>[],
              'code'=>'0',
              'abilityStar'=>$abilityStar["$source"],
          ];
          return view('web.industry.list.ajaxList', $viewData);
        }
      } catch (Exception $e) {
        $viewData = [
            'seo' => $this->returnSeoInfo('industry_index'),
            'selectedMenu' => parent::MENU_INDUSTRY_COO,
            'abilityName'=>$abilityName["$source"],
            'data'=>[],
            'code'=>'0',
            'abilityStar'=>$abilityStar["$source"],
        ];
        return view('web.industry.list.ajaxList', $viewData);
      }
    }

    public function cases(Request $request){
      $id=$request->id;
      $rqUrl=$this->apiBaseUri.$this->apiCaseUri;
      $rqData=['personal_mobile'=>'18058715658','fromRequest'=>'tanwin','pageNumber'=>1,'id'=>$id];
      $result=$this->curl->to($rqUrl)
      ->withOption('TIMEOUT', 10)
      ->withData($rqData)
      ->post();
      // print_r($result);
      // die;
      try {
        $result=json_decode($result,true);
        if($result['code']=='100'){
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'data'=>$result['data'],
              'code'=>'100'
          ];
          return view('web.industry.p_case', $viewData);
        }
        else{
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'data'=>[],
              'code'=>'0',
          ];
          return "错误，您所请求的数据不存在！";
        }
      } catch (Exception $e) {
        $viewData = [
            'seo' => $this->returnSeoInfo('industry_index'),
            'selectedMenu' => parent::MENU_INDUSTRY_COO,
            'data'=>[],
            'code'=>'0',
        ];
        return "错误，您所请求的数据不存在！";
      }
    }

    public function company(Request $request){
      $id=$request->id;
      $rqUrl=$this->apiBaseUri.$this->apiCompanyUri;
      $rqData=['personal_mobile'=>'18058715658','fromRequest'=>'tanwin','pageNumber'=>1,'enterprise_id'=>$id];
      $result=$this->curl->to($rqUrl)
      ->withOption('TIMEOUT', 10)
      ->withData($rqData)
      ->post();
      // print_r($result);
      // die;
      try {
        $result=json_decode($result,true);
        if($result['code']=='100'){
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'data'=>$result['data'],
              'code'=>'100'
          ];
          return view('web.industry.p_detail', $viewData);
        }
        else{
          $viewData = [
              'seo' => $this->returnSeoInfo('industry_index'),
              'selectedMenu' => parent::MENU_INDUSTRY_COO,
              'data'=>[],
              'code'=>'0',
          ];
          return "错误，您所请求的数据不存在！";
        }
      } catch (Exception $e) {
        $viewData = [
            'seo' => $this->returnSeoInfo('industry_index'),
            'selectedMenu' => parent::MENU_INDUSTRY_COO,
            'data'=>[],
            'code'=>'0',
        ];
        return "错误，您所请求的数据不存在！";
      }
    }

    public function articleList(Request $request){
        $pagesize=20;
        $sourceArr=['light'=>'光伏','wuni'=>'污泥'];
        $conditionArr=['policy'=>'政策法规','standard'=>'标准规范'];
        $categoryId=$request->condition;
        $pageNumber=$request->pageNum;
        $source=$request->source;
        if($pageNumber==0 || !$pageNumber)$pageNumber=1;
        $rqUrl='http://api.crm.tanwin.cn/article/ecosphereArticlePage';
        $rqData=['condition'=>$categoryId,'source'=>$source,'pageNumber'=>$pageNumber,'pageSize'=>$pagesize,'fromRequest'=>'tanwin','personal_mobile'=>'18058715658'];
        $result=$this->curl->to($rqUrl)
        ->withOption('TIMEOUT', 10)
        ->withData($rqData)
        ->post();
        // print_r('请求地址是'.$rqUrl.'</br>');
        // print_r('请求结果是'.$result);
        try {
            $result=json_decode($result,true);
            if($result['code']==100){
              $rData=$result['data']['records'];
              $data=[
                'seo' => $this->returnSeoInfo('industry_index'),
                'selectedMenu' => parent::MENU_INDUSTRY_COO,
              ];
              $data['code']=1;
              $data['data']=$rData;
              $data['count']=$result['data']['total'];
              $data['pagesize']=$pagesize;
              $data['bread_menu_name']=$sourceArr["$source"].'--'.$conditionArr["$categoryId"];
              return view('web.industry.p_law',$data);
            }
            else{
              return "服务器出错了！";
            }
          } catch (Exception $e) {
            $data['data']=null;
            $data['code']=-1;
            return "服务器出错了！";
          }
    }

    public function ajaxArticleList(Request $request){
        $categoryId=$request->condition;
        $source=$request->source;
        $rqUrl='http://api.crm.tanwin.cn/article/ecosphereArticlePage';
        $rqData=['condition'=>$categoryId,'source'=>$source,'pageNumber'=>1,'pageSize'=>5,'fromRequest'=>'tanwin','personal_mobile'=>'18058715658'];
        $result=$this->curl->to($rqUrl)
        ->withOption('TIMEOUT', 10)
        ->withData($rqData)
        ->post();
        // print_r('请求地址是'.$rqUrl.'</br>');
        // print_r('请求结果是'.$result);
        try {
            $result=json_decode($result,true);
            if($result['code']==100){
              $rData=$result['data']['records'];
              $data['code']=1;
              $data['data']=$rData;
              $data['count']=$result['data']['total'];
              $data['pagesize']='5';
              return view('web.industry.list.ajaxArticleList',$data);
            }
            else{
              return "服务器出错了！";
            }
          } catch (Exception $e) {
            $data['data']=null;
            $data['code']=-1;
            return "服务器出错了！";
          }
    }
//移动端生态圈主页
    public function indexMobile(){
      $viewData = [
          'seo' => $this->returnSeoInfo('m_industry'),
          'selectedMenu' => parent::MENU_INDUSTRY_COO,
      ];
      return view('mobile.industry.index', $viewData);
    }
//移动端合作要求
    public function indexModeMobile(){
      $viewData = [
          'seo' => $this->returnSeoInfo('m_industry'),
          'selectedMenu' => parent::MENU_INDUSTRY_COO,
      ];
      return view('mobile.industry.indexMode', $viewData);
    }

//移动端产业合作机构列表
    public function listIdsMobile(){
      $viewData = [
          'seo' => $this->returnSeoInfo('m_industry'),
          'selectedMenu' => parent::MENU_INDUSTRY_COO,
      ];
      return view('mobile.industry.list1', $viewData);
    }
//移动端金融投资机构列表
    public function listFncMobile(){
      $viewData = [
          'seo' => $this->returnSeoInfo('m_industry'),
          'selectedMenu' => parent::MENU_INDUSTRY_COO,
      ];
      return view('mobile.industry.list2', $viewData);
    }
//移动端资产机构列表
    public function assetsTradeListMobile(){
      $viewData = [
          'seo' => $this->returnSeoInfo('m_industry'),
          'selectedMenu' => parent::MENU_INDUSTRY_COO,
      ];
      return view('mobile.industry.index', $viewData);
    }
    // public function ajaxContent(){
    //     $html1='<div class="title">';
    //     $html2='</div><div class="content">';
    //     $html3='</div></div>';
    //     if(isset($_REQUEST['id'])){
    //       $companyName=$_REQUEST['id'];
    //       $viewData=[
    //           'contentTitle'=>config('front.company')[$companyName]['title'],
    //           'contentContent'=>config('front.company')[$companyName]['content'],
    //           ];
    //       $html=$html1.$viewData['contentTitle'].$html2.$viewData['contentContent'].$html3;
    //       echo $html;
    //     }
    //     else {
    //       echo '哎呀，内容丢失了!';
    //     }
    // }
    public function ajaxContent(){
        if(isset($_REQUEST['id']) && $_REQUEST['id']<=58 && $_REQUEST['id']>0){
          $id=$_REQUEST['id'];
        }
        else {
          $id=1;
        }
        $DBData = DB::table('temp_units')->where(['id'=>$id])->first();
        if($DBData->archievement=='' || $DBData->archievement==null)$DBData->archievement='暂无该项内容';
        $JSONData=json_encode($DBData);
        echo($JSONData);
    }

    public function companyMobile(){
        if(isset($_REQUEST['id']) && $_REQUEST['id']<=58 && $_REQUEST['id']>0){
          $id=$_REQUEST['id'];
        }
        else {
          $id=1;
        }
        $DBData = DB::table('temp_units')->where(['id'=>$id])->first();
        if($DBData->archievement=='' || $DBData->archievement==null)$DBData->archievement='暂无该项内容';
        $viewData = [
            'seo' => $this->returnSeoInfo('industry_index'),
            'selectedMenu' => parent::MENU_INDUSTRY_COO,
            'data'=>$DBData,
        ];
        return view('mobile.industry.company', $viewData);
    }

    public function indexMode()
    {
        $viewData=[
            'seo' => $this->returnSeoInfo('industry_index'),
            'selectedMenu' => parent::MENU_INDUSTRY_COO,
            ];
        return view('web.industry.indexMode', $viewData);
    }

    public function cacheSolutionData()
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::TAB_MENU_INDUSTRY_COO_SOLUTION);

        //solution data
        $otherConfig['limit'] = 10;
        $otherConfig['type'] = 1;
        $solutionData1 = $this->getFilterIndustrySolutionCoo($this->request, $otherConfig, $currentTabMenuList, parent::CODE_INDUSTRY_COO_SOLUTION);
        $result1 = $this->cacheGetIndustrySolutionData($solutionData1, 1);

        //solution data
        $otherConfig['limit'] = 10;
        $otherConfig['type'] = 2;
        $solutionData2 = $this->getFilterIndustrySolutionCoo($this->request, $otherConfig, $currentTabMenuList, parent::CODE_INDUSTRY_COO_SOLUTION);

        $result2 = $this->cacheGetIndustrySolutionData($solutionData2, 2);
        return [
            'solutionData1' => $result1,
            'solutionData2' => $result2,
        ];
    }

    public function cacheChainData()
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::TAB_MENU_INDUSTRY_COO_CHAIN);
        //chain data
        $otherConfig['type'] = 1;
        $chainData1 = $this->formData->getFilterIndustryChainCoo($this->request, $otherConfig, $currentTabMenuList, parent::CODE_INDUSTRY_COO_CHAIN);
        if ($chainData1 && count($chainData1['dataList']) == 5) {
            Cache::forever('industry_index_chain_data1_forever', $chainData1);
            $result1 = true;
        } else {
            $result1 = false;
        }

        //chain data
        $otherConfig['type'] = 2;
        $chainData2 = $this->formData->getFilterIndustryChainCoo($this->request, $otherConfig, $currentTabMenuList, parent::CODE_INDUSTRY_COO_CHAIN);
        if ($chainData2 && count($chainData2['dataList']) == 5) {
            Cache::forever('industry_index_chain_data2_forever', $chainData2);
            $result2 = true;
        } else {
            $result2 = false;
        }

        return [
            'chainData1' => $chainData1['dataList'],
            'result1' => $result1,
            'chainData2' => $chainData2['dataList'],
            'result2' => $result2,
        ];
    }

    public function cacheGreenResultData()
    {
        //green_result data
        $greenResultData = $this->getFilterIndustryGreenResultCoo($this->request, parent::CODE_INDUSTRY_COO_GREEN_RESULT);
        return $this->cacheGetIndustryGreenResultData($greenResultData);
    }
    /**
     * 解决方案
     * @param
     * @return [type]
     */
    public function solution()
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::TAB_MENU_INDUSTRY_COO_SOLUTION);
        $result = $this->getFilterIndustrySolutionCoo($this->request, '', $currentTabMenuList, parent::CODE_INDUSTRY_COO_SOLUTION);

        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;

        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_SOLUTION, 'flag' => $result['filterType']]),
                'currentTabMenuList' => $currentTabMenuList,
                'filter_type' => $result['filterType'],
                'pageCount' => $pageCount,
            ];
            return view('web.industry.list.solution', $viewData);
        } else {

            $javaDomainId = $currentTabMenuList[$result['filterType']]['java_flag_type'];
            $domainChildList = $this->cacheGetDomainChildrenList($javaDomainId);
            $domainIdStr = getArrayToString($domainChildList, 'id', ',');
            $techRouteFilterList = $this->domainGetTechRoute(parent::CODE_GET_DOMAIN_TECHROUTE_LIST, $domainIdStr);

            $viewData = [
                'seo' => $this->returnSeoInfo('industry_solution', $currentTabMenuList[$result['filterType']]['name']),
                'selectedMenu' => parent::MENU_INDUSTRY_COO,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_INDUSTRY_COO, parent::TAB_MENU_INDUSTRY_COO_SOLUTION],

                'currentTabMenuList' => $currentTabMenuList,
                'tabMenuMsg' => $currentTabMenuList[$result['filterType']]['msg'],

                'dataList' => $dataList,
                'filter_area' => parent::TAB_MENU_INDUSTRY_COO_SOLUTION,
                'filter_type' => $result['filterType'],

                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_SOLUTION, 'flag' => $result['filterType']]),
                'pageCount' => $pageCount,

                'techRouteFilterInfo' => $techRouteFilterList,
            ];
            return view('web.industry.solution', $viewData);
        }

    }

    /**
     * 专家列表
     * @return
     */
    public function expert()
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::TAB_MENU_INDUSTRY_COO_EXPERT);
        $result = $this->formData->getFilterIndustryExpertCoo($this->request, $currentTabMenuList, parent::CODE_INDUSTRY_COO_EXPERT);

        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;

        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_EXPERT, 'flag' => $result['filterType']]),
            ];
            return view('web.industry.list.expert', $viewData);
        } else {
            $viewData = [
                'seo' => $this->returnSeoInfo('industry_expert', $currentTabMenuList[$result['filterType']]['name']),
                'selectedMenu' => parent::MENU_INDUSTRY_COO,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_INDUSTRY_COO, parent::TAB_MENU_INDUSTRY_COO_EXPERT],

                'currentTabMenuList' => $currentTabMenuList,
                'tabMenuMsg' => $currentTabMenuList[$result['filterType']]['msg'],

                'dataList' => $dataList,
                'filter_area' => parent::TAB_MENU_INDUSTRY_COO_EXPERT,
                'filter_type' => $result['filterType'],

                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_EXPERT, 'flag' => $result['filterType']]),
                'pageCount' => $pageCount,
                'areaList' => $this->returnAreaProvinceInfo(),
            ];
            return view('web.industry.expert', $viewData);
        }
    }

    /**
     * 供应链服务
     * @return
     */
    public function chainService()
    {
        $currentTabMenuList = config('front.tab_menu.' . parent::TAB_MENU_INDUSTRY_COO_CHAIN);
        $result = $this->formData->getFilterIndustryChainCoo($this->request, '', $currentTabMenuList, parent::CODE_INDUSTRY_COO_CHAIN);
        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;

        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_CHAIN, 'flag' => $result['filterType']]),

            ];
            return view('web.industry.list.chain', $viewData);
        } else {
            $filterList = $this->formData->getTechRouteFilterList($this->request, parent::CODE_FILTER_INFO);
            $techRouteFilterList = $filterList ? $filterList['techRouteFilterList'] : [];

            $viewData = [
                'seo' => $this->returnSeoInfo('industry_chain_service', $currentTabMenuList[$result['filterType']]['name']),
                'selectedMenu' => parent::MENU_INDUSTRY_COO,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_INDUSTRY_COO, parent::TAB_MENU_INDUSTRY_COO_CHAIN],

                'currentTabMenuList' => $currentTabMenuList,
                'tabMenuMsg' => $currentTabMenuList[$result['filterType']]['msg'],

                'dataList' => $dataList,
                'filter_area' => parent::TAB_MENU_INDUSTRY_COO_CHAIN,
                'filter_type' => $result['filterType'],

                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_CHAIN, 'flag' => $result['filterType']]),
                'pageCount' => $pageCount,
                'techRouteFilterInfo' => $techRouteFilterList,

            ];
            return view('web.industry.chain_service', $viewData);
        }
    }

    /**
     * 绿色成果
     * @return
     */
    public function greenResult()
    {
        $result = $this->getFilterIndustryGreenResultCoo($this->request, parent::CODE_INDUSTRY_COO_GREEN_RESULT);
        $dataList = $result ? $result['dataList'] : [];
        $pageCount = $dataList ? $result['pageCount'] : 1;

        if ($this->request->method() == 'POST') {
            $viewData = [
                'dataList' => $dataList,
                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_GREEN_RESULT]),
            ];
            return view('web.industry.list.green_result', $viewData);
        } else {
            $viewData = [
                'seo' => $this->returnSeoInfo('industry_green_result'),
                'selectedMenu' => parent::MENU_INDUSTRY_COO,

                'breadMenuList' => $this->breadMenuList,
                'currentBreadMenuList' => [parent::MENU_INDEX, parent::MENU_INDUSTRY_COO, parent::TAB_MENU_INDUSTRY_COO_GREEN_RESULT],

                'dataList' => $dataList,
                'filter_area' => parent::TAB_MENU_INDUSTRY_COO_GREEN_RESULT,

                'infoUrl' => $this->returnInfoUrl(parent::MENU_INDUSTRY_COO, ['type' => parent::TAB_MENU_INDUSTRY_COO_GREEN_RESULT]),

                'pageCount' => $pageCount,
            ];
            return view('web.industry.green_result', $viewData);
        }
    }

}
